package com.jx.gocomtask.webservice.repository;

import java.util.Date;
import java.util.List;

import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.PagingAndSortingRepository;
import org.springframework.transaction.annotation.Transactional;

import com.jx.gocomtask.webservice.DTO.TodoStatus;
import com.jx.gocomtask.webservice.entity.ExtTaskTodo;
import com.jx.gocomtask.webservice.entity.base.CountTodo;
public interface ExtTaskTodoRepository extends PagingAndSortingRepository<ExtTaskTodo, Long> {
	public Page<ExtTaskTodo>findByTodoStatusAndFromUser(TodoStatus todoStatus,String fromUser,Pageable pageable);
	public Page<ExtTaskTodo>findByTodoStatusAndFromUserAndContentLike(TodoStatus todoStatus,String fromUser,String content,Pageable pageable);
	public Page<ExtTaskTodo>findAll(Pageable pageable);
	@Query(value = "select e FROM ExtTaskTodo e  WHERE e.gocomGroup=?1 AND e.contextFlag<10 ")
	public List<ExtTaskTodo> find10Items(String groupid);
	
	@Modifying      
	@Transactional
	@Query(value = "UPDATE ext_task_todo as e SET e.todo_status = 1  WHERE e.todo_status =  0 AND e.from_user=?1" ,nativeQuery=true)
	public void updateTodoStatus2back(String user_id);
	@Query(value = "SELECT new com.jx.gocomtask.webservice.entity.base.CountTodo(e.fromUser,COUNT(e)) "
			+ "FROM ExtTaskTodo e "
			+ "WHERE e.todoStatus =0 AND e.createTime BETWEEN ?1 AND ?2 "
			+ "GROUP BY e.fromUser")
	public List<CountTodo> countTodo(Date start,Date end);
	@Query(value = "SELECT e "
			+ "FROM ExtTaskTodo e "
			+ "WHERE e.todoStatus =0 AND e.createTime BETWEEN ?1 AND ?2 "
			+ "AND e.fromUser=?3")
	public Page<ExtTaskTodo> todayTodo(Date start,Date end,String userid,Pageable pageable);
	
	@Modifying      
	//@Transactional
	@Query(value = "UPDATE ExtTaskTodo e SET e.contextFlag =?1 WHERE e.id=?2 ")
	public void updateTaskTodo10Item(int  contextFlag,long id);
	
}